const fs = require('fs');
const path = require('path');
const readline = require('readline');

/*文件名*/
const fileName = path.join(__dirname, '../', '../', 'logs', 'access.log');
/*创建read stream*/
const readStream = fs.createReadStream(fileName);
/*创建readline*/
const rl = readline.createInterface({input: readStream});

let chromeNum = 0;
let firefoxNum = 0;
let sum = 0;

rl.on('line', lineData => {
    if (!lineData) {
        return;
    }
    // 记录总行数
    sum++;
    const arr = lineData.split(' -- ');
    if (arr[2] && arr[2].indexOf('Chrome') > 0) {
        // 累加chrome的数量
        chromeNum++;
    }
    if (arr[2] && arr[2].indexOf('Firefox') > 0) {
        // 累加Firefox的数量
        firefoxNum++;
    }
});

/*监听读取完成*/
rl.on('close', () => {
    console.log('Chrome占比：', chromeNum / sum);
    console.log('Firefox占比：', firefoxNum / sum);
});